<script>
/**
 * 品牌
 * @author:Mumu
 * @date:
 */
import Mod from './matterclasswrite'
export default {
	components:{
		Mod
	},
	data(){
		return {
			field:[{
				title: '',
				width:'30px',
				slots: { customRender: 'checked' }
			},{
				title: '#',
				width:'40px',
				customRender: ({index}) => index+1
			}, {
				title: '分类名称',
				dataIndex: 'prefixname'
			}, {
				title: '排序',
				dataIndex: 'sort',
			}, {
				title: '添加人',
				dataIndex: 'adduser',
			}, {
				title: '添加时间',
				dataIndex: 'create_time',
			}, {
				title: '操作',
				align: "center",
				width: "290px",
				dataIndex: 'action',
				fixed: 'right',
				slots: { customRender: 'action' }
			}],//表头
			list:[],//列表
			pagination: {
				defaultCurrent: 1,
				current: 1,
				defaultPageSize: 10,
				pageSize: 10,
				showQuickJumper: true,
				showSizeChanger: true,
				showTotal: (total, range) => `共${total}条`,
				total: 0,
			},
			listloading:false,
			search:{},
		}
	},
	mounted() {
		this.$nextTick(()=>{
			this.getList();
		})
	},
	methods:{
		async getList() {
			if(this.listloading) return;
			this.listloading		=	true;
			let rs	=	await this.muajax.Ysclist({
				...this.search,
				page:this.pagination.current,
				size:this.pagination.pageSize,
			});
			this.listloading		=	false;
			// if (rs.status!=200) return;
			if ( this.pagination.current == 1 ){
				this.records		=	[];
			}
			this.list=this.tosort([],rs.data,0,0,'');
			console.log(this.list)
			this.pagination.total=rs.sum;
		},//列表
		tosort(arr,list,fid=0,lev,prestring){
			list.forEach(item=>{
				if(item.fid==fid){
					item.lev=lev;
					item.prefixname=prestring+item.name;
					arr.push(item)
					this.tosort(arr,list,item.id,lev+1,prestring+'|-- ');
				}
			})
			return arr;
		},//排序
		async del(k){
			if(!await this.$showfirm('确定要删除该数据吗？')) return;
			
			let rs = await this.muajax.YscDel({
				ids:[this.list[k].id]
			});
			if(rs.status==200){
				this.$message.success('删除成功');
				if(this.list.length==1){
					this.research();
				}else{
					this.getList();
				}
				
			}
		},//删除
		research(){
			this.pagination.current=1;
			this.getList();
		},//搜索重置
		applysDataTableChangeHandle(pagination, filters, sorter) {
			this.pagination.current		= pagination.current;
			this.pagination.pageSize	= pagination.pageSize;
			this.getList();
		},//操作分页控制
	},
	computed: {

	},
}
</script>
<template>

	<a-card>
		<a-form layout="inline" class="text-center">
			<a-form-item label="搜索">
				<a-input
					v-model:value="search.keyword"
					placeholder="请输入名称或描述"
					style="width:300px"
				/>
			</a-form-item>
			<a-form-item>
				<a-space>
					<a-button type="primary" @click="research()">搜索</a-button>
					<!-- <a-button type="default">重置</a-button> -->
				</a-space>
			</a-form-item>
		</a-form>
		<div class="mm-t-20 mm-b-15">
			<a-space>
				<a-button type="primary" @click="$refs.mod.show(true,'',list)">+ 新建</a-button>
				<!-- <a-button type="primary" danger>删除</a-button> -->
			</a-space>
		</div>


		<a-table
			:columns="field"
			:data-source="list"
			:pagination="false"
			:loading="listloading"
			:scrollToFirstRowOnChange="true"
			@change="page=>applysDataTableChangeHandle(page)"
			size="small"
			:row-key="(record)=>JSON.stringify(record)"
		>
			<!-- <template #checked="{record}">
				<a-checkbox v-model:checked="record.checked"></a-checkbox>
			</template> -->
			<template #action="{record,index}">
				<a-button type="primary" @click="$refs.mod.show(true,{fid:record.id},list)">添加子类</a-button>
				<a-divider type="vertical" />
				<a-button type="primary" @click="$refs.mod.show(true,record,list)">修改</a-button>
				<a-divider type="vertical" />
				<a-button type="primary" danger @click="del(index)">删除</a-button>
			</template>
		</a-table>

	</a-card>
	<Mod ref="mod" @reload="getList()" />
</template>
<style lang="less" scoped>

</style>